home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / p4 / p4-1_2b.lha / p4-1.2b / alog / alog.c < prev    next >
C/C++ Source or Header  |  1993-01-18  |  7KB  |  269 lines

  1. #include <stdio.h>
  2. #if !defined(SYMMETRY_PTX)  &&  !defined(NCUBE)
  3. #include <strings.h>
  4. #endif
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include "alog.h"
  8.  
  9. int xx_alog_status = 0x3;    /* Logging turned ON (default) */
  10. int xx_alog_setup_called = 0;    /* setup  not yet called */
  11. int xx_alog_output_called = 0;    /* output not yet called */
  12. char xx_alog_outdir[MAX_DIRNAME_LEN+1] = "";
  13. struct head_trace_buf *xx_buf_head = NULL;
  14.  
  15.  
  16. VOID xx_write(head,pid,event,data1,data2)
  17. struct head_trace_buf *head;
  18. int pid,event,data1;
  19. char *data2;
  20. {
  21.     int xx_i, xx_j, found;
  22.     struct trace_buf *buf;
  23.  
  24.     if (xx_alog_status >> 1) {
  25.         fprintf(stderr, "ALOG: Error: event logging requested by PID %d before doing ALOG setup\n", pid);
  26.         return;
  27.     }
  28.     if (head == NULL) return;
  29.     if (head->next_entry == head->max_size +99)
  30.         return;
  31.     found = 0;
  32.     while(!found) {
  33.         if (head->next_entry >= head->max_size) {
  34.         xx_i = 1;
  35.         head->next_entry = 0;
  36.         if (head->cbuf->next_buf == NULL)
  37.             xx_i = xx_getbuf(head);
  38.         else 
  39.             head->cbuf = head->cbuf->next_buf;
  40.         if ((!xx_i) && (head->trace_flag == ALOG_WRAP))
  41.             head->cbuf = head->xx_list;
  42.         else if (!xx_i) {
  43.             head->next_entry = (head->max_size + 99);
  44.             return;
  45.         }
  46.         } /* endif */
  47.         buf = head->cbuf;
  48.         xx_i = head->next_entry;
  49.         head->next_entry++;
  50.         if (buf->ALOG_table[xx_i].event > (-1))
  51.         found = 1;
  52.     } /* end while */
  53.     buf->ALOG_table[xx_i].id     = pid;
  54.     buf->ALOG_table[xx_i].task_id     = 0;
  55.     buf->ALOG_table[xx_i].event     = event;
  56.     buf->ALOG_table[xx_i].data_int    = data1;
  57.     if (event == (-11)){
  58.         buf->ALOG_table[xx_i].tstamp = atol(data2);
  59.         buf->ALOG_table[xx_i].tind = 0;
  60.         strcpy(buf->ALOG_table[xx_i].data_string," ");
  61.         head->prev_time = 0;
  62.     } else if (event < 0) {
  63.         buf->ALOG_table[xx_i].tstamp = 0; 
  64.         buf->ALOG_table[xx_i].tind = 0;
  65.         head->prev_time = 0;
  66.         strncpy(buf->ALOG_table[xx_i].data_string,data2,MAX_LOG_STRING_LEN);
  67.         buf->ALOG_table[xx_i].data_string[MAX_LOG_STRING_LEN] = '\0';
  68.     } else {
  69.         buf->ALOG_table[xx_i].tstamp = (unsigned long) usc_clock(); 
  70.         if (buf->ALOG_table[xx_i].tstamp < head->prev_time)
  71.             head->ind_time++;
  72.         buf->ALOG_table[xx_i].tind = head->ind_time;
  73.         head->prev_time = buf->ALOG_table[xx_i].tstamp;
  74.         strncpy(buf->ALOG_table[xx_i].data_string,data2,MAX_LOG_STRING_LEN);
  75.         buf->ALOG_table[xx_i].data_string[MAX_LOG_STRING_LEN] = '\0';
  76.     }
  77. }
  78.  
  79.  
  80. VOID xx_dump(head)
  81. struct head_trace_buf *head;
  82. {
  83.     struct trace_buf *temp;
  84.     FILE *fp;
  85.     int cycle;
  86.     register int xx_i;
  87.  
  88.     if (head == NULL) return;
  89.     if (head->file_t == NULL) {
  90.         fprintf(stderr,"ALOG: *** Trace file not written ***\n");
  91.         return;
  92.     }
  93.     fp = head->file_t;
  94.     temp = head->xx_list;
  95.     for(xx_i = 0; xx_i< head->max_size; xx_i++)
  96.         if (temp->ALOG_table[xx_i].event < 0)
  97.             fprintf(fp,"%d %d %d %d %u %u %s\n",
  98.                 temp->ALOG_table[xx_i].event,
  99.                 temp->ALOG_table[xx_i].id,
  100.                 temp->ALOG_table[xx_i].task_id,
  101.                 temp->ALOG_table[xx_i].data_int,
  102.                 temp->ALOG_table[xx_i].tind,
  103.                 temp->ALOG_table[xx_i].tstamp,
  104.                 temp->ALOG_table[xx_i].data_string);
  105.  
  106.     if (head->trace_flag == ALOG_WRAP) {
  107.         temp = head->cbuf;
  108.         if (temp != NULL)
  109.             xx_dump_aux(temp,fp,head->next_entry,head->max_size);
  110.         cycle = 0;
  111.         while((temp != NULL) && (!cycle)) {
  112.             temp = temp->next_buf;
  113.             if (temp == NULL)
  114.                 temp = head->xx_list;
  115.             if (temp == head->cbuf)
  116.                 cycle = 1;
  117.             else
  118.                 xx_dump_aux(temp,fp,0,head->max_size);
  119.         };
  120.         if (temp != NULL)
  121.             xx_dump_aux(temp,fp,0,head->next_entry);
  122.     } else {
  123.         temp = head->xx_list;
  124.         while(temp != NULL) {
  125.             xx_dump_aux(temp,fp,0,head->max_size);
  126.             temp = temp->next_buf;
  127.         };
  128.     };
  129.  
  130.     fclose(head->file_t);
  131. }
  132.  
  133.  
  134. VOID xx_dump_aux(buf,fp,xx_j,xx_k)
  135. struct trace_buf *buf;
  136. FILE *fp;
  137. int xx_j, xx_k;
  138. {
  139.     register int xx_i;
  140.     for(xx_i = xx_j; xx_i < xx_k; xx_i++)
  141.         if ((buf->ALOG_table[xx_i].id != (-1)) && 
  142.             (buf->ALOG_table[xx_i].event >= 0)) 
  143.             fprintf(fp,"%d %d %d %d %u %u %s\n",
  144.                 buf->ALOG_table[xx_i].event,
  145.                 buf->ALOG_table[xx_i].id,
  146.                 buf->ALOG_table[xx_i].task_id,
  147.                 buf->ALOG_table[xx_i].data_int,
  148.                 buf->ALOG_table[xx_i].tind,
  149.                 buf->ALOG_table[xx_i].tstamp,
  150.                 buf->ALOG_table[xx_i].data_string);
  151. }
  152.  
  153.  
  154. int xx_getbuf(head)
  155. struct head_trace_buf *head;
  156. {
  157.     register int i;
  158.     struct trace_buf *temp;
  159.  
  160.     if (head == NULL) return(0);
  161.     temp = (struct trace_buf*) malloc(sizeof(struct trace_buf));
  162.     if (temp == NULL)
  163.     {
  164.         fprintf(stderr,"alog out of memory\n");
  165.         return(0);
  166.     }
  167.     else {    
  168.         head->cbuf = temp;
  169.         for(i=0;i<MAX_BUF_SIZE;i++)
  170.             temp->ALOG_table[i].id = (-1);
  171.         temp->next_buf = NULL;
  172.         if (head->xx_list == NULL)
  173.             head->xx_list = temp;
  174.         else {
  175.             temp = head->xx_list;
  176.             while(temp->next_buf != NULL)
  177.                 temp = temp->next_buf;
  178.             temp->next_buf = head->cbuf;
  179.         };
  180.         return(1);
  181.     };
  182. }
  183.  
  184.  
  185. VOID xx_user(head,id)
  186. struct head_trace_buf *head;
  187. int id;
  188. {
  189.     char c[MAX_LOG_STRING_LEN], cd[100];
  190.  
  191.     if (head == NULL) return;
  192.     if (!strcmp(xx_alog_outdir, ""))
  193.         getwd(cd);
  194.     else
  195.         strcpy(cd, xx_alog_outdir);
  196.     if (cd[strlen(cd)-1] != '/')
  197.         strcat(cd, "/");
  198.     strcat(cd,ALOG_LOGFILE);
  199.     sprintf(c,"%d",id);
  200.     strcat(cd,c);
  201.  
  202.     if ((head->file_t = fopen(cd,"w")) == NULL)
  203.         fprintf(stderr,"ALOG: *** Trace file creation failure ***\n");
  204. }
  205.  
  206.  
  207. VOID xx_user1(head,id)
  208. struct head_trace_buf *head;
  209. int id;
  210. {
  211.     struct stat buf;
  212.     char c[MAX_LOG_STRING_LEN], cd[100], x[100];
  213.  
  214.     if (head == NULL) return;
  215.     if (!strcmp(xx_alog_outdir, ""))
  216.         getwd(cd);
  217.     else
  218.         strcpy(cd, xx_alog_outdir);
  219.     if (cd[strlen(cd)-1] != '/')
  220.         strcat(cd, "/");
  221.     strcat(cd,ALOG_LOGFILE);
  222.     sprintf(c,"%d",id);
  223.     strcat(cd,c);
  224.  
  225.     stat(cd,&buf);
  226.     strcpy(cd,(char *) ctime(&buf.st_ctime));
  227.  
  228.     strcpy(x,"AL");
  229.     strncat(x,cd+4,3);    
  230.     strcat(x,"-");
  231.     strncat(x,cd+8,2);    
  232.     strcat(x,"-");
  233.     strncat(x,cd+22,2);    
  234.  
  235.     strcpy(c,"            ");
  236.     strncpy(c,x,12);
  237.     xx_write(head,0,(-1),0,c);
  238.  
  239.     sprintf(x,"%lu", (unsigned long) usc_rollover_val());
  240.     xx_write(head,0,(-11),0,x);
  241. }
  242.  
  243.  
  244. VOID xx_alog_setup(pid,flag)
  245. int pid, flag;
  246. {
  247.     register int i;
  248.     char c[MAX_LOG_STRING_LEN], cd[MAX_LOG_STRING_LEN];
  249.  
  250.     usc_init(); 
  251.     xx_alog_status &= 0x1;    /* set initialized flag */
  252.     xx_buf_head = (struct head_trace_buf *)
  253.                 malloc(sizeof(struct head_trace_buf));
  254.     if (xx_buf_head == NULL) {
  255.             fprintf(stderr,"ALOG: *** Trace buffer HEADER creation failure;\n");
  256.             fprintf(stderr,"      *** tracing is being disabled.\n");
  257.         return;
  258.     }
  259.     xx_buf_head->next_entry = 0;
  260.     xx_buf_head->max_size = MAX_BUF_SIZE;
  261.     xx_buf_head->prev_time = 0;
  262.     xx_buf_head->ind_time = 0;
  263.     xx_buf_head->trace_flag = flag;
  264.     xx_buf_head->xx_list = NULL;
  265.     if (!(xx_getbuf(xx_buf_head)))
  266.             printf("ALOG: **** trace buffer creation failure ****\n");
  267.     xx_user(xx_buf_head,(pid));
  268. }
  269.